var shop_common = {};
var app = getApp();

// 取出缓存
shop_common.getStorage = (key) => {
	return new Promise((resolve, reject) => {
		wx.getStorage({
			key,
			success: (res) => {
				resolve(res.data)
			},
			fail: () => {
				reject()
			}
		})
	})
}

// 设置缓存
shop_common.setStorage = (key, data) => {
	return new Promise((resolve, reject) => {
		wx.setStorage({
			key,
			data,
			success: () => {
				resolve()
			}
		})
	})
}

// 清空购物车
shop_common.deleteStoreNum = (_this) => {
	console.log('9999999999999999')
	wx.removeStorage({
		key: 'store' + _this.store_id
	})
	wx.removeStorage({
		key: 'shop_cart_' + _this.store_id
	})

	console.log('888888888888888')
	shop_common.getStorage('storeId_list').then((data) => {
		console.log('777777777777777', data)
		data = data.filter(item => item != _this.store_id)
		shop_common.setStorage('storeId_list', data)
	})
}

// 商品加入购物车缓存
shop_common.setStoreInfoStorage = (_this) => {
	if (_this.store.selectGoods && _this.store.selectGoods.length > 0) {
		var storeInfo = {
			name: _this.storeInfo.name,
			store_id: _this.storeInfo.store_id,
			delivery: _this.storeInfo.delivery,
			delivery_price: _this.storeInfo.delivery_price,
			avatar: _this.storeInfo.image,
			is_close: _this.storeInfo.is_close,
		}

		shop_common.setStorage('store' + _this.store_id, {
			selectGoods: _this.store.selectGoods,
			selectType: _this.store.selectType,
			goodsNumList: _this.store.goodsNumList,
			totalNum: _this.store.totalNum,
			selectAdd: _this.store.selectAdd,
			storeInfo: storeInfo
		})

		shop_common.getStorage('storeId_list').then((storeId_list) => {
			var id = ''
			storeId_list = storeId_list.filter(item => item != _this.store_id)
			storeId_list.unshift(_this.store_id)
			if (storeId_list.length > 20) {
				id = storeId_list[20]
				wx.removeStorage({
					key: 'store' + id
				})
				storeId_list = storeId_list.slice(0, 20)
			}
			shop_common.setStorage('storeId_list', storeId_list)
		}, () => {
			shop_common.setStorage('storeId_list', [_this.store_id])
		})
	} else {
		wx.removeStorage({
			key: 'store' + _this.store_id
		})
		wx.removeStorage({
			key: 'shop_cart_' + _this.store_id
		})
	}
}

// 点击加减按钮
shop_common.getGoods = (e, _this) => {
	var index = -1
	console.log('9999999999888', e)
	// #ifdef H5
	var goods = JSON.parse(JSON.stringify(e))
	// #endif

	// #ifdef MP-WEIXIN
	var goods = JSON.parse(JSON.stringify(e.detail))
	// #endif
	console.log('9999999999888', e, goods)
	var selectGoods = _this.store.selectGoods ? _this.store.selectGoods : []
	var selectType = {}
	var goodsNumList = {}
	var goodsKeyList = {}
	var goodsSpecList = {}
	var totalNum = 0

	if (goods.subsidiary == 1 && !goods.status) {
		_this.selectFormat({
			product_id: goods.product_id
		})
		return
	}

	if (goods.subsidiary == 1 && goods.status) {
		_this.selectComponent('#shopcart').openCart()
		return
	}

	if (goods.age_status) {
		_this.age_status = goods.age_status,
		_this.show_age = true
		return
	}

	if (goods.is_login) {
		var user_age = app.globalData.userInfo.age * 1
		var limit_age = _this.storeInfo.check_age_object.age_limit_buy * 1
		if (user_age <= 0) {
			_this.age_status = 1
		} else if (user_age < limit_age) {
			_this.age_status = 2
		} else {
			if (_this.showSpecMask) {
				_this.selectComponent('#spec').countAdd()
			} else if (_this.subsidiaryAddInfo) {
				_this.subsidiaryAdd(_this.subsidiaryAddInfo)
			} else if (_this.selectComponent('#count-' + _this.needAddId)) {
				if (_this.isfrom != '') {
					_this.selectComponent('#shopcart').selectAddBtn(_this.needAddId)
				} else {
					_this.selectComponent('#count-' + _this.needAddId).add()
				}
			} else {
				_this.addToCart()
			}
		}
		return
	}

	if (goods.needLogin) {
		_this.needAddId = goods.goods_id
		_this.isfrom = goods.isfrom
		_this.needLogin = true
		return
	}

	// 购物车的商品数组
	for (var i = 0; i < selectGoods.length; i++) {
		if (selectGoods[i].key == goods.key) {
			index = i
			break
		}
	}
	if (index === -1) {
		selectGoods = selectGoods.concat(goods)
	} else {
		if (goods.selectNum == 0) {
			selectGoods.splice(index, 1)
		} else {
			selectGoods[index] = goods
			// selectGoods.splice(index, 1, goods)
		}
	}



	// 左侧商品列表数字角标对象
	for (var i = 0; i < selectGoods.length; i++) {
		var sort_id = selectGoods[i].sort_id
		var product_id = selectGoods[i].product_id

		selectType[sort_id] = selectType[sort_id] ? selectType[sort_id] + selectGoods[i].selectNum : selectGoods[i].selectNum
		goodsNumList[product_id] = goodsNumList[product_id] ? goodsNumList[product_id] + selectGoods[i].selectNum :
			selectGoods[i].selectNum
		goodsKeyList[selectGoods[i].key] = selectGoods[i].selectNum
		totalNum = totalNum + selectGoods[i].selectNum

		if (selectGoods[i].specId && selectGoods[i].specId != '') {
			goodsSpecList[selectGoods[i].specId] = goodsSpecList[selectGoods[i].specId] ? goodsSpecList[selectGoods[i].specId] +
				selectGoods[i].selectNum : selectGoods[i].selectNum
		} else {
			goodsSpecList[product_id] = goodsNumList[product_id]
		}
	}

	if (selectGoods.length === 0) {
		shop_common.deleteStoreNum(_this)
	}

	var arr = []
	_this.store.selectGoods = arr.concat(selectGoods)
	_this.store.selectType = selectType
	_this.store.goodsNumList = goodsNumList
	_this.store.totalNum = totalNum
	_this.goodsKeyList = goodsKeyList
	_this.goodsSpecList = goodsSpecList


	// 将商品数据缓存
	shop_common.setStoreInfoStorage(_this)

}

// 获取当前店铺商品缓存
shop_common.getStoreData = (_this) => {
	shop_common.getStorage('store' + _this.store_id).then((data) => {
		_this.store = data
		if (data.selectGoods) {
			var goodsSpecList = {}
			var goodsKeyList = {}
			for (var i = 0; i < data.selectGoods.length; i++) {
				var goods = data.selectGoods[i]
				goodsKeyList[goods.key] = goods.selectNum

				if (goods.specId && goods.specId != '') {
					goodsSpecList[goods.specId] = goodsSpecList[goods.specId] ? goodsSpecList[goods.specId] + goods.selectNum :
						goods.selectNum
				} else {
					goodsSpecList[goods.product_id] = data.goodsNumList[goods.product_id]
				}
			}
			_this.goodsKeyList = goodsKeyList
			_this.goodsSpecList
		}
	}, () => {
		_this.store = {
			goodsNumList: {}
		}
		_this.goodsKeyList = {}
		_this.goodsSpecList = {}
	})
}

module.exports = shop_common
